package com.supermarket.manage.provider.util;

import com.supermarket.manage.common.constant.RedisKeyConstant;
import com.supermarket.manage.common.utils.TimeUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 单据的id生成工具 22位
 *
 * @author Faxon
 * @version 1.0
 * @date 2020-03-14 15:49
 **/
@Component
public class IDUtil {
    /**
     * 采购入库id前缀
     */
    private static final String PURCHASE_IN = "PI";

    /**
     * 销售退货入库id前缀
     */
    private static final String RETURN_IN = "RI";

    /**
     * 销售出库id前缀
     */
    private static final String SALE_OUT = "SO";

    /**
     * 采购退货出库id前缀
     */
    private static final String BACK_OUT = "BO";

    /**
     * 盘点id前缀
     */
    private static final String STOCKING = "SI";

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 采购入库id
     *
     * @return
     */
    public String getPurchaseInId() {
        return PURCHASE_IN + TimeUtil.getIdTime() +
                getIncr(RedisKeyConstant.PURCHASE_IN) +
                getRandomNumber();
    }

    /**
     * 销售退货入库id
     *
     * @return
     */
    public String getReturnInId() {
        return RETURN_IN + TimeUtil.getIdTime() +
                getIncr(RedisKeyConstant.RETURN_IN) +
                getRandomNumber();
    }

    /**
     * 销售出库id
     *
     * @return
     */
    public String getSaleOutId() {
        return SALE_OUT + TimeUtil.getIdTime() +
                getIncr(RedisKeyConstant.SALE_OUT) +
                getRandomNumber();
    }

    /**
     * 采购退货出库id
     *
     * @return
     */
    public String getBackOutId() {
        return BACK_OUT + TimeUtil.getIdTime() +
                getIncr(RedisKeyConstant.BACK_OUT) +
                getRandomNumber();
    }

    /**
     * 盘点id
     *
     * @return
     */
    public String getStockingId() {
        return STOCKING + TimeUtil.getIdTime() +
                getIncr(RedisKeyConstant.STOCKING) +
                getRandomNumber();
    }

    /**
     * 获取32位的uuid
     *
     * @return String
     */
    public String getUUID() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replace("-", "");
    }


    /**
     * 获取 3位 自增长序列号
     *
     * @param key
     * @return
     */
    private String getIncr(String key) {
        RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        long increment = entityIdCounter.getAndIncrement();
        if (increment == 0) {
            entityIdCounter.expire(getCurrent2TodayEndMillisTime(), TimeUnit.MILLISECONDS);//单位毫秒
        }
        if (increment >= 999) {
            redisTemplate.delete(key);
        }
        String result;
        if (increment < 10) {
            result = "00" + increment;
        } else if (increment >= 10 && increment < 100) {
            result = "0" + increment;
        } else {
            result = String.valueOf(increment);
        }
        return result;
    }

    /**
     * 现在到今天剩余的毫秒数
     *
     * @return
     */
    private Long getCurrent2TodayEndMillisTime() {
        Calendar todayEnd = Calendar.getInstance();
        // Calendar.HOUR 12小时制
        // HOUR_OF_DAY 24小时制
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTimeInMillis() - System.currentTimeMillis();
    }

    /**
     * 获取 2 位 随机数
     *
     * @return
     */
    private String getRandomNumber() {
        Random random = new Random();
        return String.valueOf(random.nextInt(10)) + random.nextInt(10);
    }
}
